#include "card.h"
#include "scene.h"

CardTexture *CardTexture::texture = nullptr;
CardTexture::Deleter CardTexture::deleter;

Card::Card(CardType type, int number, QGraphicsItem *parent)
    :QObject(nullptr), QGraphicsItem(parent), type_(type), number_(number)
{
    setCacheMode(DeviceCoordinateCache);
    texture = CardTexture::instance();
    bounding_rect = QRectF(-texture->card_front.width() / 2, -texture->card_front.height() / 2,
                           texture->card_front.width(), texture->card_front.height());
    state = Front;
}

QRectF Card::boundingRect() const
{
    return bounding_rect;
}

void Card::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
    switch (type_) {
    case Coins:
        drawCard(painter, texture->large_coins[number_ - 1],
                 texture->small_coins, QColor(174, 43, 19));
        break;
    case Bamboo:
        drawCard(painter, texture->large_bamboo[number_ - 1],
                 texture->small_bamboo, QColor(29, 117, 83));
        break;
    case Characters:
        drawCard(painter, texture->large_characters[number_ - 1],
                 texture->small_characters, QColor(0, 0, 0));
        break;
    case Green:
        drawCard(painter, texture->large_green, texture->small_green);
        break;
    case White:
        drawCard(painter, texture->large_white, texture->small_white);
        break;
    case Red:
        drawCard(painter, texture->large_red, texture->small_red);
        break;
    case Flower:
        drawCard(painter, texture->large_flower, texture->small_flower);
        break;
    default:
        break;
    }
}

QString Card::str()
{
    switch (type_) {
    case Coins: return "Coins" + QString::number(number_);
    case Bamboo: return "Bamboo" + QString::number(number_);
    case Characters: return "Characters" + QString::number(number_);
    case Green: return "Green" + QString::number(number_);
    case White: return "White" + QString::number(number_);
    case Red: return "Red" + QString::number(number_);
    case Flower: return "Flower" + QString::number(number_);
    default: return "";
    }
}

void Card::drawCard(QPainter *painter, const QPixmap &large,
                    const QPixmap &small, const QColor color)
{
    int left = -bounding_rect.width() / 2;
    int top = -bounding_rect.height() / 2;

    if (state == Front)
    {
        painter->drawPixmap(left, top, texture->card_shadow);
        painter->drawPixmap(left, top, texture->card_front);
        painter->drawPixmap(left, top, texture->card_texture);
        switch (type_) {
        case Coins:
        case Bamboo:
        case Characters:
        {
            painter->drawPixmap(-large.width() / 2, -large.height() / 2 - 3, large);
            QFont font("Glacial Indifference", 15, QFont::ExtraBold);
            font.setHintingPreference(QFont::PreferNoHinting);
            painter->setFont(font);
            painter->setPen(color);
            painter->drawText(QRectF(left + 9, top + 7, 16, 16),
                              Qt::AlignCenter, QString::number(number_));
            painter->drawPixmap(left + 10, top + 16 + 10, small);
            painter->save();
            painter->translate(0, -2);
            painter->scale(-1, -1);
            painter->translate(0, 2);
            painter->drawText(QRectF(left + 9, top + 7, 16, 16),
                              Qt::AlignCenter, QString::number(number_));
            painter->drawPixmap(left + 10, top + 16 + 10, small);
            painter->restore();
        }
        break;
        case Green:
        case White:
        case Red:
        case Flower:
        {
            painter->drawPixmap(-large.width() / 2, -large.height() / 2 - 3, large);
            painter->drawPixmap(left + 7, top + 7, small);
            painter->save();
            painter->translate(0, -2);
            painter->scale(-1, -1);
            painter->translate(0, 2);
            painter->drawPixmap(left + 7, top + 7, small);
            painter->restore();
        }
        break;
        default:
            break;
        }
    }
    else
    {
        painter->drawPixmap(left, top, texture->card_shadow);
        if (Scene::winCount() > 100)
        {
            painter->drawPixmap(left, top, texture->card_back_upgrade);
        }
        else
        {
            painter->drawPixmap(left, top, texture->card_back);
        }
    }
}
